﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using FxSuperCore.Log;
using FxWinSuper.Functions;
using FxWinSuper.Plugin.Extension;
using FxWinSuper.UI.Controls;
using Application = System.Windows.Forms.Application;

namespace FxWinSuper.Util
{
	public static class ApplicationUtil
	{
		private static ILogPrint Logger = InternalLog.Instance;

		public static event CancelEventHandler BeforeClose;

		public static event EventHandler Closed;

		static ApplicationUtil()
		{
			MainWindow.Instance.Closing += (sender, args) => BeforeClose?.Invoke(sender, args);
			MainWindow.Instance.Closed += (sender, args) => Closed?.Invoke(sender, args);
		}

		public static void HideMainWindow()
		{
			MainWindow.Instance.Invoke(() =>
			{
				MainWindow.Instance.Hide();
			});
		}

		public static void ShowMainWindow()
		{
			MainWindow.Instance.Invoke(() =>
			{
				MainWindow.Instance.Show();
			});
		}

		public static void ActiveMainWindow()
		{
			MainWindow.Instance.Invoke(() =>
			{
				MainWindow.Instance.Activate();
			});
		}

		public static IntPtr GetMainWindowHandle()
		{
			return MainWindow.Instance.WinHandle;
		}

		/// <summary>
		/// 延迟注册一个page页，当在要用到该page页时才初始化
		/// </summary>
		/// <param name="pageTitle">页标题</param>
		/// <param name="icon">图标</param>
		/// <param name="getPage">延迟获取页的函数</param>
		public static void InstallPage(string pageTitle, Image icon, Func<Page> getPage)
		{
			PluginHelper.AssertAccessThreadIsMain();
			MainWindow.Instance.RegisterSliderMenu(pageTitle, icon, getPage);
		}

		/// <summary>
		/// 注册一个page页
		/// </summary>
		/// <param name="pageTitle">页标题</param>
		/// <param name="icon">图标</param>
		/// <param name="page">待注册的页对象</param>
		public static void InstallPage(string pageTitle, Image icon, Page page)
		{
			PluginHelper.AssertAccessThreadIsMain();
			MainWindow.Instance.RegisterSliderMenu(pageTitle, icon, page);
		}

		/// <summary>
		/// 切换页面
		/// </summary>
		/// <param name="PageTitle">页面标题</param>
		public static void SwitchPage(string PageTitle)
		{
			PluginHelper.RunOnUiThread(() =>
			{
				MainWindow.Instance.SwitchPage(PageTitle);
			});
		}

		/// <summary>
		/// 注册一个托盘右键菜单子项
		/// </summary>
		/// <param name="subItem">子组件</param>
		public static void RegisterContextMenuItem(UIElement subItem)
		{
			PluginHelper.AssertAccessThreadIsMain();
			MainWindow.Instance.RegisterContextMenuItem(subItem);
		}

		/// <summary>
		/// 注册一个托盘右键菜单子项
		/// </summary>
		/// <param name="subItem">子组件</param>
		public static void RegisterContextMenuItem(FxContextMenuItem subItem)
		{
			PluginHelper.AssertAccessThreadIsMain();
			MainWindow.Instance.RegisterContextMenuItem(subItem);
		}

		/// <summary>
		/// 关闭托盘图标已经展开的菜单项
		/// </summary>
		public static void CloseNotifyIconContextMenu()
		{
			MainWindow.Instance.Invoke(() =>
			{
				MainWindow.Instance.CloseNotifyIconContextMenu();
			});
		}

		/// <summary>
		/// 退出程序
		/// </summary>
		public static void Exit(int code = 0)
		{
			Logger.Info($"程序主动调用退出，code={code}");
			MainWindow.Instance.Invoke(() =>
			{
				MainWindow.Instance.Close();
			});
			Environment.ExitCode = code;
		}

		/// <summary>
		/// 重启程序
		/// </summary>
		/// <param name="delay">重启延时</param>
		public static void Restart(int delay = 2)
		{
			Logger.Info($"准备重启程序");
			string path = Path.GetDirectoryName(Application.ExecutablePath);
			string exeName = Path.GetFileName(Application.ExecutablePath);
			Process process = new Process();
			process.StartInfo.FileName = "cmd.exe";
			process.StartInfo.Arguments = $"/c timeout /t {delay} && cd \"{path}\" && {exeName}";
			process.StartInfo.CreateNoWindow = true;
			process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
			process.StartInfo.UseShellExecute = true;
			process.StartInfo.Verb = "runas";
			process.Start();
			Exit(0);
		}
	}
}
